There is a problem with dplyr and plotly. When loaded tofether both have functions of same name so must point to the library you reall need, i.e. dplyr::select()
#library(reticulate)
library(tidyverse)
library(plotly)
library(janitor)
library(readxl)
player_projection <- read_csv("DFF_NHL_cheatsheet.csv", col_names = TRUE)
slice takes the top 3 grouped records
later I ungroup and combine field names.
Then I filter out points above 60 to create a new object for graphing
team__reg_line <- player_projection %>%
clean_names() %>%
mutate_if(is.numeric, ~replace_na(., 0)) %>%
filter(!position == "G") %>%
filter(!reg_line == 0) %>%
select(team, reg_line, l5_ppg_max, salary) %>%
arrange(team, reg_line, desc(l5_ppg_max)) %>%
group_by(team, reg_line) %>%
dplyr::slice(1:3) %>%
dplyr::summarise(total_pts = sum(l5_ppg_max), salary = sum(salary)) %>%
dplyr::ungroup() %>%
unite(team_line, team, reg_line, sep = "_", remove = TRUE)
## `summarise()` has grouped output by 'team'. You can override using the
## `.groups` argument.
team_pts_max_20 <- team__reg_line %>%
dplyr::filter(total_pts >= 60)
this is called a lollypop chart
# install.packages("ggplot2")
library(ggplot2)
ggplot(team_pts_max_20, aes(x = team_line, y = salary)) +
geom_segment(aes(x = reorder(team_line, total_pts), xend = team_line, y = 5000, yend = salary)) +
geom_point() +
coord_flip() +
geom_point(size = 12, pch = 21, bg = 4, col = 1) +
geom_text(aes(label = round(total_pts)), color = "white", size = 3)
p <- ggplot(team_pts_max_20, aes(x = team_line, y = salary)) +
geom_segment(aes(x = reorder(team_line, total_pts), xend = team_line, y = 5000, yend = salary)) +
geom_point() +
coord_flip() +
geom_point(size = 12, pch = 21, bg = 4, col = 1) +
geom_text(aes(label = round(total_pts)), color = "white", size = 3)
ggplotly(p)